package com.dalongxin.work.page.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.dalongxin.work.R;
import com.dalongxin.work.data.Contact;
import com.dalongxin.work.view.CircularImage;

import java.util.List;

public class ContactAdapter extends BaseRecyclerAdapter<ContactAdapter.ContactViewHolder> {

    private List<Contact> mData;

    public ContactAdapter(List<Contact> mData) {
        this.mData = mData;
    }

    @Override
    public ContactViewHolder getViewHolder(View view) {
        return new ContactViewHolder(view, true);
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact_single, parent, false);
        return new ContactViewHolder(v, true);
    }

    @Override
    public void onBindViewHolder(ContactViewHolder holder, int position, boolean isItem) {
        if(mData != null) {
            Contact item = mData.get(position);
            if (item != null) {
                holder.contactName.setText(item.getUserName());
                holder.contactPhone.setText(item.getPhone());
            }
        }
    }

    @Override
    public int getAdapterItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {
        private CircularImage avatar;
        private TextView contactName;
        private TextView contactPhone;
        public ContactViewHolder(View itemView, boolean isItem) {
            super(itemView);
            avatar = itemView.findViewById(R.id.contact_avatar);
            contactName = itemView.findViewById(R.id.contact_name);
            contactPhone = itemView.findViewById(R.id.contact_phone);
        }
    }
}
